import requests
import time

class MarkingMachine:
    
    CHARACTER_TYPE = "utf-8"
    POST_PARAMETER = "filename"
    TIMEOUT_SECONDS = 30
    QUERY_STATUSES = {'200,数据标刻成功！', '201,未开始标刻'}

    def __init__(self, url, filename):
        self.url = url
        self.file = filename

    def print_QR_code(self):
        data = {self.POST_PARAMETER: self.file}
        marking_response = requests.post(self.url, data=data)
        marking_response.encoding = self.CHARACTER_TYPE
        
        print("开始打标")
        print(self.__wait_for_print_success())
        return 1

    def __wait_for_print_success(self):
        start_time = time.time()
        while True:
            query_response = requests.get(self.url)
            query_response.encoding = self.CHARACTER_TYPE
            time.sleep(2)
            if query_response.text in self.QUERY_STATUSES:
                return query_response.text
            elapsed_time = time.time() - start_time
            if elapsed_time > self.TIMEOUT_SECONDS:
                return "打标失败：等待超时"

if __name__ == '__main__':
    # 创建MarkingMachine对象并进行打标操作
    url = "http://192.168.1.101:9999/printimage"
    filename = "233.ezd"
    marking_machine = MarkingMachine(url, filename)
    marking_machine.print_QR_code()